package com.anhry.app.safety.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @date:2011-8-25
 * @remark:
 * @author:dongbin
 */
public class Test {

	public static void main(String arg[]) {
		Test a = new Test();
		System.out.print("请输入一个十进制整数:");
		try

		{
			BufferedReader br = new BufferedReader(new InputStreamReader(
					System.in));
			int i = Integer.parseInt(br.readLine());
			System.out.print("该数的二进制数是：");
			a.BinaryString(i);
			System.out.println();
			System.out.print("该数的八进制数是：");
			a.OctalString(i);
			System.out.println();
			System.out.print("该数的十六进制数是：");
			a.HexString(i);
			System.out.println();
			//System.out.println(Integer.valueOf("101101", 2).toString());// 2进制转换为10进制
		} catch (IOException e) {
			System.err.println(e.toString());
		}
	}

	public void BinaryString(int x) {
		if (x > 0)
			BinaryString(x / 2);
		else
			return;
		System.out.print(x % 2);
	}

	public void OctalString(int x) {
		if (x > 0)
			OctalString(x / 8);
		else
			return;
		System.out.print(x % 8);
	}

	public void HexString(int x) {
		if (x > 0)
			BinaryString(x / 16);
		else
			return;
		if (x % 16 == 10) {
			System.out.print("A");
		} else if (x % 16 == 11) {
			System.out.print("B");
		} else if (x % 16 == 12) {
			System.out.print("C");
		} else if (x % 16 == 13) {
			System.out.print("D");
		} else if (x % 16 == 14) {
			System.out.print("E");
		} else if (x % 16 == 15) {
			System.out.print("F");
		} else
			System.out.print(x % 16);
	}
}
